package com.yimu.lib.http.download;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;



import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 图片下载
 *
 */
public class DownLoadImageService implements Runnable {
    private String url;
    private Context context;
    private ImageDownLoadCallBack callBack;
    private File currentFile;
    public DownLoadImageService(Context context, String url, ImageDownLoadCallBack callBack) {
        this.url = url;
        this.callBack = callBack;
        this.context = context;
    }

    @Override
    public void run() {
        URL imgUrl = null;
        Bitmap bitmap = null;
        try {
            imgUrl = new URL(url);
            // 使用HttpURLConnection打开连接
            HttpURLConnection urlConn = (HttpURLConnection) imgUrl
                    .openConnection();
            urlConn.setDoInput(true);
            urlConn.connect();
            // 将得到的数据转化成InputStream
            InputStream is = urlConn.getInputStream();
            // 将InputStream转换成Bitmap
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
            savePhotoToSDCard(bitmap, Environment.getExternalStorageDirectory().getAbsolutePath()
                    + File.separator+getFileName(url));


        } catch (MalformedURLException e) {
            System.out.println("[getNetWorkBitmap->]MalformedURLException");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("[getNetWorkBitmap->]IOException");
            e.printStackTrace();
        }
    }

    /**
     * 文件名
     *
     * @param path
     * @return
     */
    private static String getFileName(String path) {
        String imageName = path.substring(path.lastIndexOf("/") + 1, path.length());
        return imageName;

    }
    /**
     * Save image to the SD card
     * @param photoBitmap
     * @param path
     */
    private  void savePhotoToSDCard(Bitmap photoBitmap,String path){
        File photoFile;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

            photoFile = new File(path);
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(photoFile);
                if (photoBitmap != null) {
                    if (photoBitmap.compress(Bitmap.CompressFormat.JPEG, 70, fileOutputStream)) {
                        callBack.onDownLoadSuccess(photoFile);
                        fileOutputStream.flush();
                    }else {
                        callBack.onDownLoadFailed();
                    }
                }
            } catch (Exception e) {
                photoFile.delete();
                e.printStackTrace();
            } finally{
                try {
                    fileOutputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }
    }
}
